//
//  RCShopManageViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-10-2.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCShopManageViewController.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "IManager+ModuleShopManageDetail.h"
#import "RCShopManageDetailCell.h"
#import "GloableEngineService.h"
#import "AppDelegate.h"
#import "RCMerchantPictureViewController.h"
#import "FrameworkRelativeDefines.h"
#import "RCShopDetailModifyViewController.h"
#import "NSArray+EX.h"

@interface RCShopManageViewController ()<IManagerDelegate,UITableViewDataSource,UITableViewDelegate,RCShopManageDetailHeaderCellDelegate,RCShopManageModifyDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) RCShopManagerDataModel *dataModel;
@property (nonatomic, assign) int64_t requestID;
@property (nonatomic, retain) MBProgressHUDEx *progressHub;


@end

@implementation RCShopManageViewController

@synthesize dataModel = _dataModel;
@synthesize tableView= _tableView;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;

- (void) dealloc
{
    self.dataModel = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self refreshData];
    self.titleLabelText = @"店铺管理";
    self.navigationItem.titleView = [self createTitleView];
    
    
    
    CGRect rect = CGRectMake(0, 0, 320, UIScreenHeight-64);
    _tableView = [[UITableView alloc] initWithFrame:rect];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.viewContainer addSubview:_tableView];
    
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma
#pragma mark --- UITableView

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (nil == self.dataModel || nil == self.dataModel.merchantDetailItems || [self.dataModel.merchantDetailItems count] == 0) {
        return 0;
    }
    return [self.dataModel.merchantDetailItems count] +1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0) {
        return 130;
    }else{
        
        RCShopManagerDetailItem *item = nil;
        if ([indexPath row] < [self.dataModel.merchantDetailItems count] +1) {
            item = [self.dataModel.merchantDetailItems objectAtIndex:([indexPath row]-1)];
        }
        
        if (item.type == kShopManagerDetailType_Phone || item.type == kShopManagerDetailType_Address) {
            
            return [RCShopManageDetailOnelineCell cellHeightWithString:item];
        }else{
            return [RCShopManageDetailTwolineCell cellHeightWithString:item.content];
        }
        return 60;
    }
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([indexPath row] == 0) {
        RCShopManageDetailHeaderCell *cell = [[RCShopManageDetailHeaderCell alloc] init];
        cell.delegate = self;
        [cell bindData:self.dataModel];
        return [cell autorelease];
    }
    RCShopManagerDetailItem *item = nil;
    if ([indexPath row] < [self.dataModel.merchantDetailItems count] +1) {
        item = [self.dataModel.merchantDetailItems objectAtIndex:([indexPath row]-1)];
    }
    if (item.type == kShopManagerDetailType_Phone || item.type == kShopManagerDetailType_Address ) {
        RCShopManageDetailOnelineCell *oneLineCell = [[RCShopManageDetailOnelineCell alloc] init];
        UIView *backgroundBGView = [[[UIView alloc]initWithFrame:oneLineCell.bounds]autorelease];
        backgroundBGView.backgroundColor = [UIColor colorWithHex:0xffe7e7e7];
        oneLineCell.selectedBackgroundView = backgroundBGView;
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
    }else {
        RCShopManageDetailTwolineCell *twoLineCell = [[RCShopManageDetailTwolineCell alloc] init];
        
        UIView *backgroundBGView = [[[UIView alloc]initWithFrame:twoLineCell.bounds]autorelease];
        backgroundBGView.backgroundColor = [UIColor colorWithHex:0xffe7e7e7];
        twoLineCell.selectedBackgroundView = backgroundBGView;
        [twoLineCell bindData:item];
        
        return [twoLineCell autorelease];
    }
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if ([indexPath row] == 0) {
        return;
    }
    
    RCShopManagerDetailItem *item = nil;
    if ([indexPath row] < [self.dataModel.merchantDetailItems count] +1) {
        item = [self.dataModel.merchantDetailItems objectAtIndex:([indexPath row]-1)];
    }
    
    if (item.type == kShopManagerDetailType_Address || item.type == kShopManagerDetailType_Phone || item.type == kShopManagerDetailType_Opentime|| item.type == kShopManagerDetailType_Description) {
        
        RCShopDetailModifyViewController *vc = [[RCShopDetailModifyViewController alloc]initWithDataModel:item];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
}
#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance] requestShopManageDetailInfo:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        self.dataModel = (RCShopManagerDataModel *)dataModel;
        [_tableView reloadData];
    }else if (_requestID == kInvalidRequestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
    
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId || _requestID == kInvalidRequestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
}

#pragma WriteSignin and Comment Delegate



-(void)gotoPictureAlbumView{
    if ([GloableEngineService sharedInstance].netWorkStatus == kNotReachable) {
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    
    NSString *shopID = [GloableEngineService sharedInstance].shopID;
    
    RCMerchantPictureViewController *vc = [[RCMerchantPictureViewController alloc]initWithShopID:shopID andShopName:@"相册"];
    vc.isFromServer = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)modifySuccessed:(NSString *)field withContent:(NSString *)content{
    if (self.dataModel && self.dataModel.merchantDetailItems) {
        for (int i =0; i < [self.dataModel.merchantDetailItems count]; i++) {
            RCShopManagerDetailItem *item = [self.dataModel.merchantDetailItems safeObjectAtIndex:i];
            if (nil == item) {
                continue;
            }
            if ([item.field isEqualToString:field]) {
                item.content = content;
                break;
            }
        }
        [self.tableView reloadData];
    }
}

@end
